//
//  NSDate+QSExtension.m
//  testDemo
//
//  Created by apple on 2017/6/23.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "NSDate+QSExtension.h"

@implementation NSDate (QSExtension)
/**
 *  NSDate转NSString
 */
- (NSString *)stringFromDate:(NSDate *)date withDateFormat:(NSString *)dateFormat
{
    //获取系统当前时间
    NSDate *currentDate = date;
    //用于格式化NSDate对象
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //设置格式：zzz表示时区
    [dateFormatter setDateFormat:dateFormat];
    //NSDate转NSString
    NSString *currentDateString = [dateFormatter stringFromDate:currentDate];
    return currentDateString;
}

/**
 *  NSString转NSDate
 */
- (NSDate *)dateFromString:(NSString *)string withDateFormat:(NSString *)dateFormat
{
    //需要转换的字符串
    NSString *dateString = string;
    //设置转换格式
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ;
    [formatter setDateFormat:dateFormat];
    //NSString转NSDate
    NSDate *date=[formatter dateFromString:dateString];
    return date;
}

/**
 *  将世界时间转化为中国区时间
 */
+ (NSDate *)worldTimeToChinaTime:(NSDate *)date{
    
    NSTimeZone *timeZone = [NSTimeZone systemTimeZone];
    NSInteger interval = [timeZone secondsFromGMTForDate:date];
    NSDate *localeDate = [date  dateByAddingTimeInterval:interval];
    return localeDate;
}

/**
 *  计算两个日期的时间间隔
 */
- (NSMutableDictionary *)calculateTimeIntervalfromDateOne:(NSString *)dateOne toDateTwo:(NSString *)dateTwo withDateFormatter:(NSString *)dateFormatterStr
{
    // 首先创建格式化对象
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:dateFormatterStr];
    
    NSDate *date1 = [dateFormatter dateFromString:dateOne];
    NSDate *date2 = [dateFormatter dateFromString:dateTwo];
    
    // 计算时间间隔（单位是秒）
    NSTimeInterval time = [date1 timeIntervalSinceDate:date2];
    
    // 计算天数、时、分、秒
    int days = ((int)time)/(3600*24);
    int hours = ((int)time)%(3600*24)/3600;
    int minutes = ((int)time)%(3600*24)%3600/60;
    int seconds = ((int)time)%(3600*24)%3600%60;
    
    //（%i可以自动将输入转换为十进制,而%d则不会进行转换）
    NSMutableDictionary *tempDict = [NSMutableDictionary dictionary];
    [tempDict setObject:[NSString stringWithFormat:@"%i", days] forKey:@"Day"];
    [tempDict setObject:[NSString stringWithFormat:@"%i", hours] forKey:@"Hour"];
    [tempDict setObject:[NSString stringWithFormat:@"%i", minutes] forKey:@"Minute"];
    [tempDict setObject:[NSString stringWithFormat:@"%i", seconds] forKey:@"Second"];
    
    return tempDict;
}

/**
 *  比较2个日期差
 */
-(NSDateComponents *)intervalToDate:(NSDate *)date{
    // 日历对象
    NSCalendar *calender = [NSCalendar currentCalendar];
    
    // 获得一个时间元素
    NSCalendarUnit  unit =  NSCalendarUnitYear | NSCalendarUnitMonth
    | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute |NSCalendarUnitSecond;
    
    return [calender components:unit fromDate:self toDate:date options:kNilOptions];
}

/**
 *  与当前时间比较
 */
-(NSDateComponents *)intervalToNow{
    
    return [self intervalToDate:[NSDate date]];
}

/**
 *  获取年
 */
-(NSString *)yearByDate{
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    
    NSDateComponents *components = [calendar components:NSCalendarUnitYear fromDate:self];
    return [NSString stringWithFormat:@"%04zd",[components year]];
}

/**
 *  获取月
 */
-(NSString *)monthByDate{
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:NSCalendarUnitMonth fromDate:self];
    return [NSString stringWithFormat:@"%02zd",[components month]];
}

/**
 *  获取日
 */
-(NSString *)dayByDate{
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:NSCalendarUnitDay fromDate:self];
    return [NSString stringWithFormat:@"%02zd",[components day]];
}

/**
 *  获取时分
 */
- (NSString *)hourMinByDate{
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:NSCalendarUnitHour | NSCalendarUnitMinute fromDate:self];
    return [NSString stringWithFormat:@"%02zd : %02zd",[components hour],[components minute]];
}

/**
 *  获取星期
 */
-(NSString *)weekdayByDate{
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:NSCalendarUnitWeekday fromDate:self];
    long weekNum = [components weekday];
    NSString *weekDay = nil;
    switch (weekNum) {
        case 1:
            weekDay=@"星期日";
            break;
        case 2:
            weekDay=@"星期一";
            break;
        case 3:
            weekDay=@"星期二";
            break;
        case 4:
            weekDay=@"星期三";
            break;
        case 5:
            weekDay=@"星期四";
            break;
        case 6:
            weekDay=@"星期五";
            break;
        case 7:
            weekDay=@"星期六";
            break;
            
        default:
            break;
    }
    return weekDay;
}

/**
 *  获取上一年
 */
-(NSDate *)getLastYear{
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth fromDate:self];
    components.year -= 1;
    return [calendar dateFromComponents:components];
}

/**
 *  获取下一年
 */
-(NSDate *)getNextYear{
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth fromDate:self];
    components.year += 1;
    return [calendar dateFromComponents:components];
}

/**
 *  获取上一个月
 */
-(NSDate *)getLastMonth{
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth fromDate:self];
    components.month -= 1;
    return [calendar dateFromComponents:components];
}

/**
 *  获取下一个月
 */
-(NSDate *)getNextMonth{
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth fromDate:self];
    components.month += 1;
    return [calendar dateFromComponents:components];
}

/**
 *  获取上一日
 */
-(NSDate *)getLastDay{
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:self];
    components.day -= 1;
    return [calendar dateFromComponents:components];
}

/**
 *  获取下一日
 */
- (NSDate *)getNextDay{
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:self];
    components.day += 1;
    return [calendar dateFromComponents:components];
}

/**
 *  是否是今天
 */
- (BOOL)isToday
{
    NSString *dateOne = [self stringFromDate:self withDateFormat:@"yyyy-MM-dd"];
    NSString *dateTwo = [self stringFromDate:[[NSDate alloc] init] withDateFormat:@"yyyy-MM-dd"];
    
    NSDictionary *dict = [self calculateTimeIntervalfromDateOne:dateOne toDateTwo:dateTwo withDateFormatter:@"yyyy-MM-dd"];
    NSString *days = [dict valueForKey:@"Day"];
    
    return days == 0;
}

/**
 *  时间戳转换为时间
 */
+ (NSDate *)timeStampChangeToDate:(NSString *)timeStamp
{
    // 毫秒值转化为秒
    NSDate* date = [NSDate dateWithTimeIntervalSince1970:[timeStamp doubleValue] / 1000.0];
    return date;
}
@end
